/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.talbd.proventas;

import edu.upn.evolucion.proventas.entidades.Proveedor;
import edu.upn.evolucion.proventas.negocio.ProveedorNEG;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Administrador
 */
public class GestionarProveedor extends javax.swing.JInternalFrame {

    /**
     * @return the id
     */
    public static int getId() {
        return id;
    }

    /**
     * @param aId the id to set
     */
    public static void setId(int aId) {
        id = aId;
    }

    /**
     * @return the razonsocial
     */
    public static String getRazonsocial() {
        return razonsocial;
    }

    /**
     * @param aRazonsocial the razonsocial to set
     */
    public static void setRazonsocial(String aRazonsocial) {
        razonsocial = aRazonsocial;
    }

    /**
     * @return the direccion
     */
    public static String getDireccion() {
        return direccion;
    }

    /**
     * @param aDireccion the direccion to set
     */
    public static void setDireccion(String aDireccion) {
        direccion = aDireccion;
    }

    /**
     * @return the email
     */
    public static String getEmail() {
        return email;
    }

    /**
     * @param aEmail the email to set
     */
    public static void setEmail(String aEmail) {
        email = aEmail;
    }

    /**
     * @return the observaciones
     */
    public static String getObservaciones() {
        return observaciones;
    }

    /**
     * @param aObservaciones the observaciones to set
     */
    public static void setObservaciones(String aObservaciones) {
        observaciones = aObservaciones;
    }

    /**
     * Creates new form GestionarProveedor
     */
    public GestionarProveedor() {
        initComponents();
    }

    private static int id;
    private static String razonsocial;
    private static String direccion;
    private static String email;
    private static String observaciones;
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProveedores = new javax.swing.JTable();
        txtNombreBuscarProveedor = new javax.swing.JTextField();
        btnBuscarProveedor1 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        lblRestriccionSeleccion = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Gestionar Proveedor");

        jLabel5.setText("Nombre:");

        tblProveedores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Razon Social", "Direccion", "Email", "Observaciones"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblProveedores);

        btnBuscarProveedor1.setText("Buscar");
        btnBuscarProveedor1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarProveedor1ActionPerformed(evt);
            }
        });

        jButton1.setText("Registrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        lblRestriccionSeleccion.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(txtNombreBuscarProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnBuscarProveedor1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(btnEliminar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnModificar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(lblRestriccionSeleccion)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtNombreBuscarProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscarProveedor1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(11, 11, 11)
                        .addComponent(btnModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblRestriccionSeleccion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarProveedor1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarProveedor1ActionPerformed
        // TODO add your handling code here:
        ArrayList<Proveedor> coleccion = new ArrayList<Proveedor>();
        String nombreBuscar = getTxtNombreBuscarProveedor().getText();

        Proveedor objProveedor = new Proveedor();
        objProveedor.setRazonsocial(nombreBuscar);
        coleccion = ProveedorNEG.obtenerInstancia().buscar(objProveedor);
        DefaultTableModel temp = (DefaultTableModel) getTblProveedores().getModel();
        temp.setRowCount(0);
        for(Proveedor p : coleccion){
            //System.out.println(“Nombre del Empleado: ” + tmpObjeto.getNombre());
            Object nuevo[] ={p.getId(), p.getRazonsocial(), p.getDireccion(), p.getEmail(), p.getObservaciones()};
            temp.addRow(nuevo);
        }
    }//GEN-LAST:event_btnBuscarProveedor1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        RegistrarProveedor ventana = new RegistrarProveedor(null, closable);
        ventana.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        // TODO add your handling code here:
         // TODO add your handling code here:
        TableModel tableModel = getTblProveedores().getModel();
        int fila = getTblProveedores().getSelectedRow();
        if(fila == -1){
            lblRestriccionSeleccion.setText("*Seleccione");
        }
        else{
         lblRestriccionSeleccion.setText("");
        setId(Integer.parseInt(tableModel.getValueAt(fila, 0).toString()));
        setRazonsocial(tableModel.getValueAt(fila, 1).toString());
        setDireccion(tableModel.getValueAt(fila, 2).toString());
        if(tableModel.getValueAt(fila, 3) == null){
            setEmail("");
        }
        else{
            setEmail(tableModel.getValueAt(fila, 3).toString());
        }
        if(tableModel.getValueAt(fila, 4) == null){
            setObservaciones("");
        }
        else{
            setObservaciones(tableModel.getValueAt(fila, 4).toString());
        }
        
        
        ModificarProveedor ventana = new ModificarProveedor(null, closable);
        ventana.setVisible(true);
        }
        //setSeleccion(new Cliente(Integer.parseInt(tableModel.getValueAt(fila, 0).toString()), tableModel.getValueAt(fila, 1).toString(), tableModel.getValueAt(fila, 2).toString(), tableModel.getValueAt(fila, 3).toString(), tableModel.getValueAt(fila, 4).toString(), tableModel.getValueAt(fila, 5).toString(), tableModel.getValueAt(fila, 6).toString(), null, tableModel.getValueAt(fila, 7).toString()));
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
        int fila = getTblProveedores().getSelectedRow();
        if(fila == -1){
            lblRestriccionSeleccion.setText("*Seleccione");
        }
        else{
         lblRestriccionSeleccion.setText("");
        int idProveedor = Integer.parseInt(getTblProveedores().getValueAt(fila, 0).toString());
        String msg =ProveedorNEG.obtenerInstancia().eliminar(idProveedor);
        JOptionPane.showMessageDialog(null, msg);
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarProveedor1;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblRestriccionSeleccion;
    private javax.swing.JTable tblProveedores;
    private javax.swing.JTextField txtNombreBuscarProveedor;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the btnBuscarProveedor1
     */
    public javax.swing.JButton getBtnBuscarProveedor1() {
        return btnBuscarProveedor1;
    }

    /**
     * @param btnBuscarProveedor1 the btnBuscarProveedor1 to set
     */
    public void setBtnBuscarProveedor1(javax.swing.JButton btnBuscarProveedor1) {
        this.btnBuscarProveedor1 = btnBuscarProveedor1;
    }

    /**
     * @return the btnEliminar
     */
    public javax.swing.JButton getBtnEliminar() {
        return btnEliminar;
    }

    /**
     * @param btnEliminar the btnEliminar to set
     */
    public void setBtnEliminar(javax.swing.JButton btnEliminar) {
        this.btnEliminar = btnEliminar;
    }

    /**
     * @return the btnModificar
     */
    public javax.swing.JButton getBtnModificar() {
        return btnModificar;
    }

    /**
     * @param btnModificar the btnModificar to set
     */
    public void setBtnModificar(javax.swing.JButton btnModificar) {
        this.btnModificar = btnModificar;
    }

    /**
     * @return the jButton1
     */
    public javax.swing.JButton getjButton1() {
        return jButton1;
    }

    /**
     * @param jButton1 the jButton1 to set
     */
    public void setjButton1(javax.swing.JButton jButton1) {
        this.jButton1 = jButton1;
    }

    /**
     * @return the jLabel5
     */
    public javax.swing.JLabel getjLabel5() {
        return jLabel5;
    }

    /**
     * @param jLabel5 the jLabel5 to set
     */
    public void setjLabel5(javax.swing.JLabel jLabel5) {
        this.jLabel5 = jLabel5;
    }

    /**
     * @return the jScrollPane1
     */
    public javax.swing.JScrollPane getjScrollPane1() {
        return jScrollPane1;
    }

    /**
     * @param jScrollPane1 the jScrollPane1 to set
     */
    public void setjScrollPane1(javax.swing.JScrollPane jScrollPane1) {
        this.jScrollPane1 = jScrollPane1;
    }

    /**
     * @return the tblProveedores
     */
    public javax.swing.JTable getTblProveedores() {
        return tblProveedores;
    }

    /**
     * @param tblProveedores the tblProveedores to set
     */
    public void setTblProveedores(javax.swing.JTable tblProveedores) {
        this.tblProveedores = tblProveedores;
    }

    /**
     * @return the txtNombreBuscarProveedor
     */
    public javax.swing.JTextField getTxtNombreBuscarProveedor() {
        return txtNombreBuscarProveedor;
    }

    /**
     * @param txtNombreBuscarProveedor the txtNombreBuscarProveedor to set
     */
    public void setTxtNombreBuscarProveedor(javax.swing.JTextField txtNombreBuscarProveedor) {
        this.txtNombreBuscarProveedor = txtNombreBuscarProveedor;
    }
}
